﻿/*
*localStorage&sessionStorage 使用帮助类 1.0
*/

(function (window) {
    var storage = window.localStorage;
    //判断是否支持本地存储
    if (!storage) {
        return;
    }
    _lStorage = {
        //设置键值
        setItem: function (key, value) {
            storage.setItem(key, value);
        },
        //获取键对应的值
        getItem: function (key) {
            return storage.getItem(key);
        },
        //获取json对象
        getJSON: function (key) {
            var val = this.getItem(key);
            return $.evalJSON(val);
        },
        //设置json对象
        setJSON: function (key, value) {
            this.setItem(key, $.toJSON(value));
        },
        //删除键和值
        removeItem: function (key) {
            storage.removeItem(key);
        },


        //获取所有的键，返回数组
        getKeys: function () {
            var keys = [];
            for (var i = 0; i < storage.length; i++) {
                keys.push(storage.key(i));
            }
            return keys;
        },
        //获取当前域中所有的键和值,返回键值数组
        getKeyValues: function () {
            var result = [];
            for (var i = 0; i < storage.length; i++) {
                result.push({ key: storage.key(i), value: storage.getItem(storage.key(i)) });
            }
            return result;
        },
        //批量设置键和值,指定key和value的数组
        setKeyValues: function (keyValues) {
            for (var i = 0; i < keyValues.length; i++) {
                var item = keyValues[i];
                this.setItem(item.key, item.value);
            }
        }
    }
    LStorage = window.LStorage = _lStorage;




    var sStorage = window.sessionStorage;
    _sStorage = {
        //设置键值
        setItem: function (key, value) {
            sStorage.setItem(key, value);
        },
        //获取键对应的值
        getValue: function (key) {
            return sStorage.getItem(key);
        },
        //设置键和json对象
        setJSON: function (key, obj) {
            var jsonStr = $.toJSON(obj);
            this.setItem(key, jsonStr);
        },
        //获取键对应的json对象
        getJSON: function (key) {
            return $.evalJSON(this.getValue(key));
        },
        //删除键和值
        removeItem: function (key) {
            sStorage.removeItem(key);
        },
        //清空所有的键和值
        clear: function () {
            sStorage.clear();
        },

        //获取所有的键，返回数组
        getKeys: function () {
            var keys = [];
            for (var i = 0; i < sStorage.length; i++) {
                keys.push(sStorage.key(i))
            }
            return keys;
        },
        //获取当前域中所有的键值数组
        getKeyValues: function () {
            var result = [];
            var keys = this.getKeys();
            for (var i = 0; i < keys.length; i++) {
                result.push({ key: keys[i], value: this.getValue(keys[i]) });
            }
            return result;
        },
        //批量设置键和值
        setKeyValues: function (keyValues) {
            for (var i = 0; i < keyValues.length; i++) {
                var item = keyValues[i];
                this.setItem(item.key, item.value);
            }
        }
    }
    SStorage = window.SStorage = _sStorage;
})(window);


